//
//  YearPicker.swift
//  HuaYun
//
//  Created by enghou on 2018/7/26.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

import UIKit

class YearPicker: UIView,UIPickerViewDelegate,UIPickerViewDataSource {
    
    var showtotal = false
    
    var years:Array<Int> = []
    
    var selectedYear: Int {
        
        get{
            let index = self.datePicker.selectedRow(inComponent: 0)
            if index < years.count && index > -1 {
                return years[index]
            }
            return -1
        }
    }
    
    
    
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if self.showtotal == true {
            return years.count + 1
        } else {
            return years.count
        }
    }
    
    func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        
        return 40
        
    }
    
    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
        
        let label = UILabel()
        
        label.font = UIFont.systemFont(ofSize: 18)
        
        label.textColor = .black
        
        label.textAlignment = .center
        
        if row < years.count {
            label.text = String(years[row])
        } else if row == years.count {
            label.text = "合计"
        }
        return label
    }
    
    
    let datePicker = UIPickerView()

    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        datePicker.backgroundColor = UIColor.white
        
        datePicker.dataSource = self
        
        datePicker.delegate = self
        
        self.addSubview(datePicker)
        
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        datePicker.frame = self.bounds
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
